// author: lbfamous
// file: wxLogin.js
// desc: 
// date: 2020-01-27

const GameData = require("GameData");

cc.Class({
    extends: cc.Component,

    properties: {

    },

    // onLoad () {},

    start () {

        if(cc.sys.platform == cc.sys.WECHAT_GAME) {

            wx.login({
                success(res) {
                    if (res.code) {
                        //发起网络请求
                        wx.request({
                          url: "https://www.lbfamous.icu:18001/token?code=" + res.code,
                          method: "POST",
                          success: function(data) {
                              if(data.statusCode == 200){
                                  wx.postMessage({
                                      message:"SetOpenId",
                                      openid: data.data.openid
                                  });
                              }
                          }                         
                        });

                        wx.showShareMenu();

                        wx.onShareAppMessage((res) => {
                            return {
                                title:"来和小猫一起钓鱼！",
                                imageUrl:"https://www.lbfamous.icu/share/share.png",
                                success(res) {
                                    console.log("转发成功！");
                                },
                                fail(res){
                                    console.log("转发失败! ");
                                }
                                
                            }
                        });
                      }else {
                        console.log('登录失败！' + res.errMsg)
                      }                      
                }
            })
        }
    },

    // update (dt) {},


    onShareClick() {
    
        //这个函数一直没响应，不知道为什么
        //原来是不能带 success 和 fail 函数
        
        if(cc.sys.platform == cc.sys.WECHAT_GAME){
            console.log("onShareClick() ->",wx);
            wx.shareAppMessage({                
                    title:"不服来战，我的小猫拿到了 "+ GameData.instance.score + "分！",
                    imageUrl: "https://www.lbfamous.icu/share/share.png",                   
            })
        }
    }
});
